const sequelize = require("@/model/sequelizeBaseConfig");
const { Op } = require("sequelize");
const z_base = {
  /**
   * 单条数据查询
   * @param {*} Model 查询的模型
   * @param {Object} condition 查询的条件传递对象，因为设计查询的属性需要动态传递
   * @returns
   */
  async GetOne(Model, condition) {
    /* 
      findOne方法对sql语句方法，底层调用select，根据UserAuth模型定义查询具体的表和数据项，只不过会添加limit=1限制
    */
    try {
      let userAuth = await Model.findOne({
        where: condition,
      });

      return userAuth.dataValues;
    } catch (error) {
      return;
    }
  },
  /**
   * // [单行]更新: 传入对应结构体[传递主键用] 和 带有对应更新字段值的[结构体]，结构体不更新零值
   * @param {*} Model 更新的模型
   * @param {*} data 更新的数据项
   * @param {*} condition 更新的条件
   */
  async Update(Model, data, condition = {}) {
    try {
      await Model.update(data, {
        where: condition,
      });
    } catch (error) {
      console.log("更新数据发生错误", error);
    }
  },
  async Create(Modal, data) {
    try {
      let r = await Modal.create(data);
      return r.dataValues;
    } catch (error) {
      console.log("创建失败", error);
    }
  },
  async Update(Modal, data, condition) {
    try {
      await Modal.update(data, { where: condition });
    } catch (error) {
      console.log("更新失败", error);
    }
  },
  //批量更新
  async Updates(Modal, data, condition) {
    try {
      await Modal.update(data, { where: condition });
    } catch (error) {
      console.log("批量更新失败", error);
    }
  },
  async Delete(Modal, condition) {
    try {
      await Modal.destroy({ where: condition });
    } catch (error) {
      console.log("删除失败", error);
    }
  },
  /**
   *
   * @param {*} Modal
   * @param {*} query 条件
   * @param {*} args 数据
   */
  async Count(Modal, query, args) {
    let total = 0;
    try {
      if (query) {
        //条件存在
        total = await Modal.count({
          where: sequelize.literal(query),
          replacements: { ids: args }, //传递的args是数组
        });
      } else {
        total = await Modal.count();
      }
      return total;
    } catch (error) {
      console.log("统计失败", error);
      return -1;
    }
  },
  async Count2(Modal, wherelause) {
    try {
      const count = Modal.count({ where: wherelause });
      return count;
    } catch (error) {
      console.log("count2统计失败", error);
    }
  },
  async UpdatesMap(Modal, maps, condition) {
    try {
      await Modal.update(maps, { where: condition });
    } catch (error) {
      console.log("UpdatesMap失败", error);
    }
  },
  async List(Modal) {
    try {
      let result = await Modal.findAll();
      result = result.map((item) => {
        return item.dataValues;
      });
      return result;
    } catch (error) {
      console.log("获取数据列表失败", error);
    }
  },
  /**
   *跟list查询一直，只不过这里添加条件
   * @returns
   */
  async ListByCondition(model, attr, keyword) {
    try {
      const result = await model.findAll({
        where: {
          [attr]: {
            [Op.like]: `%${keyword}%`,
          },
        },
      });
      return result;
    } catch (error) {
      console.log("获取数据列表失败", error);
    }
  },
  async bulkCreate(model, list) {
    try {
      await model.bulkCreate(list);
    } catch (error) {
      console.log("bulkCreate失败", error);
    }
  },
};

module.exports = z_base;
